<div class="panel panel-default panel-intro">
    {:build_heading()}

    <div class="panel-body">
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="one">
                <div class="widget-body no-padding">
                    <form id="generate-form" class="edit-form form-horizontal" role="form" data-toggle="validator" method="POST" action="">
                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2">选择任务:</label>
                            <div class="col-xs-12 col-sm-8">
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th width="50">
                                                    <input type="checkbox" id="check-all">
                                                </th>
                                                <th>订单号</th>
                                                <th>产品型号</th>
                                                <th>工序</th>
                                                <th>员工</th>
                                                <th>分配数量</th>
                                                <th>已报数量</th>
                                                <th>剩余数量</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {volist name="allocations" id="allocation"}
                                            <tr>
                                                <td>
                                                    <input type="checkbox" name="allocation_ids[]" value="{$allocation.id}" class="allocation-check">
                                                </td>
                                                <td>{$allocation.order_no}</td>
                                                <td>{$allocation.product_name} - {$allocation.model_name}</td>
                                                <td>{$allocation.process_name}</td>
                                                <td>{$allocation.employee_name}</td>
                                                <td>{$allocation.allocated_quantity}</td>
                                                <td>{$allocation.reported_quantity}</td>
                                                <td>{$allocation.allocated_quantity - $allocation.reported_quantity}</td>
                                            </tr>
                                            {/volist}
                                        </tbody>
                                    </table>
                                </div>
                                {empty name="allocations"}
                                <div class="text-center py-4">
                                    <i class="fa fa-inbox fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">暂无可生成二维码的任务</h5>
                                    <p class="text-muted">请先分配任务给员工</p>
                                </div>
                                {/empty}
                            </div>
                        </div>
                        
                        <div class="form-group layer-footer">
                            <label class="control-label col-xs-12 col-sm-2"></label>
                            <div class="col-xs-12 col-sm-8">
                                <button type="submit" class="btn btn-success btn-embossed disabled" id="generate-btn">
                                    <i class="fa fa-qrcode"></i> 生成二维码
                                </button>
                                <button type="reset" class="btn btn-default btn-embossed">
                                    <i class="fa fa-refresh"></i> 重置
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    // 全选/取消全选
    $('#check-all').change(function() {
        $('.allocation-check').prop('checked', $(this).prop('checked'));
        updateGenerateBtn();
    });
    
    // 单个选择
    $(document).on('change', '.allocation-check', function() {
        updateGenerateBtn();
        
        // 检查是否全选
        var total = $('.allocation-check').length;
        var checked = $('.allocation-check:checked').length;
        $('#check-all').prop('checked', total === checked);
    });
    
    // 更新生成按钮状态
    function updateGenerateBtn() {
        var checked = $('.allocation-check:checked').length;
        if (checked > 0) {
            $('#generate-btn').removeClass('disabled').text('生成二维码 (' + checked + ')');
        } else {
            $('#generate-btn').addClass('disabled').text('生成二维码');
        }
    }
    
    // 表单提交
    $('#generate-form').on('submit', function(e) {
        e.preventDefault();
        
        var checked = $('.allocation-check:checked');
        if (checked.length === 0) {
            Toastr.error('请选择要生成二维码的任务');
            return;
        }
        
        var allocationIds = [];
        checked.each(function() {
            allocationIds.push($(this).val());
        });
        
        // 显示确认对话框
        layer.confirm('确定要为选中的 ' + allocationIds.length + ' 个任务生成二维码吗？', {
            icon: 3,
            title: '确认生成'
        }, function(index) {
            layer.close(index);
            
            // 显示加载层
            var loadIndex = layer.load(1, {shade: [0.3, '#000']});
            
            $.post('{:url("batchGenerate")}', {
                allocation_ids: allocationIds.join(',')
            }, function(data) {
                layer.close(loadIndex);
                
                if (data.code === 1) {
                    Toastr.success(data.msg);
                    setTimeout(function() {
                        location.href = '{:url("index")}';
                    }, 1500);
                } else {
                    Toastr.error(data.msg);
                }
            }).fail(function() {
                layer.close(loadIndex);
                Toastr.error('网络错误，请重试');
            });
        });
    });
});
</script> 